package com.demo.controller;



import com.demo.annotation.GlobalInterceptor;
import com.demo.annotation.VerifyParam;
import com.demo.entity.dto.PostSaveInfoDto;
import com.demo.entity.dto.SessionWebUserDto;

import com.demo.entity.enums.PostStatusEnum;
import com.demo.entity.po.PostInfo;
import com.demo.entity.po.UserInfo;


import com.demo.entity.query.PostInfoQuery;
import com.demo.entity.vo.PostsVO;
import com.demo.entity.vo.ResponseVO;


import com.demo.exception.BusinessException;
import com.demo.service.PostInfoService;
import com.demo.service.UserInfoService;
import com.demo.utils.CopyTools;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;




@RestController
@RequestMapping("/post")

public class PostInfoController extends ABaseController {

	@Resource
	private PostInfoService postInfoService;

	@Resource
	private UserInfoService userInfoService;



	/**
	 * 获取帖子
	 */
	@RequestMapping("/loadPostsList")
	@GlobalInterceptor
	public ResponseVO loadDataList(HttpSession session,
								   PostInfoQuery query) {
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		query.setQueryUserInfo(true);
		if (!webUserDto.getAdmin()){ // 如果不是管理员就查看所有自己的帖子
			query.setUserId(webUserDto.getUserId());
			query.setQueryUserInfo(false);
		}
		return getSuccessResponseVO(postInfoService.findListByPage(query));
	}


	/**
	 * 获取帖子
	 */
	@RequestMapping("/loadPostsList4User")
	@GlobalInterceptor
	public ResponseVO loadPostsList4User(HttpSession session,
								   PostInfoQuery query) {
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		query.setUserId(webUserDto.getUserId());
		query.setQueryUserInfo(false);
		return getSuccessResponseVO(postInfoService.findListByPage(query));
	}


	/**
	 * 加载所有的帖子
	 * @param
	 * @param query
	 * @return
	 */
	@RequestMapping("/loadAllPost")
	@GlobalInterceptor(checkLogin = false)
	public ResponseVO loadAllPost(PostInfoQuery query) {
		query.setQueryUserInfo(true);
		query.setStatus(PostStatusEnum.POSTED.getStatus());
		query.setOrderBy("update_time desc");
		return getSuccessResponseVO(postInfoService.findListByPage(query));
	}

	/**
	 * 保存文章/帖子信息
	 * @return
	 */
	@RequestMapping("/savePosts")
	@GlobalInterceptor
	public ResponseVO savePosts(HttpSession session,
								PostSaveInfoDto postSaveInfoDto){
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		postSaveInfoDto.setUserId(webUserDto.getUserId());
		PostInfo posts = CopyTools.copy(postSaveInfoDto, PostInfo.class);
		postInfoService.savePost(posts);
		return getSuccessResponseVO(null);
	}

	/**
	 * 修改状态
	 * @return
	 */
	@RequestMapping("/updateStatus")
	@GlobalInterceptor
	public ResponseVO updateStatus(HttpSession session,
								   @VerifyParam(required = true) Integer status,
								   @VerifyParam(required = true) Long id){
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		postInfoService.updateStatus(webUserDto.getUserId(),id,status);
		return getSuccessResponseVO(null);
	}

	/**
	 * 删除忒子
	 * @param session
	 * @param id 帖子id
	 * @return
	 */
	@RequestMapping("/delPost")
	@GlobalInterceptor
	public ResponseVO delPost(HttpSession session,@VerifyParam(required = true) Long id){
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		postInfoService.deletePosts(webUserDto.getUserId(),id);
		return getSuccessResponseVO(null);
	}

	/**
	 * 获取帖子
	 * @param id 帖子id
	 * @return
	 */
	@RequestMapping("/loadPost")
	@GlobalInterceptor(checkLogin = false)
	public ResponseVO loadPost(@VerifyParam(required = true) Long id){
		PostInfo postsDB = postInfoService.getPostsById(id);
		if (postsDB == null){
			throw new BusinessException("帖子信息不存在");
		}
		PostsVO  postsVO= CopyTools.copy(postsDB,PostsVO.class);
		UserInfo userInfoDb = userInfoService.getUserInfoByUserId(postsDB.getUserId());
		postsVO.setName(userInfoDb.getUsername());
		return getSuccessResponseVO(postsVO);
	}
}